﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YWTSSys.Model;
using YWTSSys.Library;
using YWTSSys.BLL;

namespace YWTSSys.Web.MachineSys.SysBasic
{
    public partial class OMenuEdit : PageBase
    {
        #region
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改};
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OMeunList";
            }
        }
        /// <summary>
        /// 需要修改的菜单ID
        /// </summary>
        protected int MenuId
        {
            get {
                return GetRequestValue<int>("menuid");
            }
        }

        private OMenuInfo _objMenu;
        protected OMenuInfo objMenu
        {
            get
            {
                if (_objMenu == null)
                    _objMenu = OMenuController.GetMenuByMenuId(MenuId);
                return _objMenu;
                //OMenuController.ge
            }
        }

        private List<OMenuPermissionInfo> _listMenuPermission;
        protected List<OMenuPermissionInfo> listMenuPermission
        {
            get
            {
                if (_listMenuPermission == null)
                    _listMenuPermission = OMenuController.GetOMenuPermissionByMenuId(MenuId);
                return _listMenuPermission;
                //OMenuController.ge
            }
        }
        #endregion
        protected string GetPermission()
        {
            string perhtml = "";
            string peritemtemp = "<label><input type=\"checkbox\" name=\"PermissionTag\" value=\"{0}\" {1} />{0}</label>";
            Array items;
            items = Enum.GetValues(typeof(Permission));
            // string item;
            foreach (Permission item in items)
            {
                string temp = "";
                foreach (OMenuPermissionInfo it in listMenuPermission)
                {
                    if (it.PermissionTag == item.ToString())
                    {
                        temp = "checked";
                        break;
                    }
                }

                perhtml = perhtml + string.Format(peritemtemp, item.ToString(), temp);
            }
            return perhtml;
        }
        protected string GetPermission2()
        {
            string perhtml = "";
            string peritemtemp = "<label><input type=\"checkbox\" name=\"PermissionTag\" value=\"{0}\" {1} />{0}</label>";
          
          
                foreach (OMenuPermissionInfo it in listMenuPermission)
                {
                    perhtml = perhtml + string.Format(peritemtemp, it.PermissionTag.ToString(),"checked");
                }

               
           
            return perhtml;
        }
        protected string GetParMenuId()
        {
            string perhtml = "";
            string peritemtemp = "<option value=\"{0}\" style=\"font-weight:{3};\" {5} >{2}{4}{1}</option>";
            List<OMenuInfo> alltop = OMenuController.GetAllParentMenu(0);
            // string item;
            perhtml = perhtml + "<option value='0' " + (objMenu.ParMenuId == 0 ? "selected" : "") + ">顶级菜单...</option>";
            foreach (OMenuInfo item in alltop)
            {
                perhtml = perhtml + string.Format(peritemtemp, item.MenuId, item.MenuName, "", "bold", item.DispayIndex, item.MenuId == objMenu.ParMenuId ? "selected" : "");

                List<OMenuInfo> alltop2 = OMenuController.GetAllParentMenu(item.MenuId);
                foreach (OMenuInfo item2 in alltop2)
                {
                    perhtml = perhtml + string.Format(peritemtemp, item2.MenuId, item2.MenuName, "-->", "", item2.DispayIndex, item2.MenuId == objMenu.ParMenuId ? "selected" : "");
                }
            }
            return perhtml;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "editmenu")
            {
                editmenu();
            }
        }

        private void editmenu()
        {
            OMenuInfo obj = Request.SerializableObject<OMenuInfo>();
           // obj.MenuId = MenuId;
            string newPerTag = Request["PermissionTag"];
           // newPerTag = string.IsNullOrEmpty(newPerTag) ? "" : newPerTag;
            List<string> oldPerTagList = new List<string>();
            foreach (OMenuPermissionInfo item in listMenuPermission)
            {
                oldPerTagList.Add(item.PermissionTag);
            }


            if (string.IsNullOrEmpty(newPerTag))
            {
                //1.从原权限与新权限对比
                foreach (string temp in oldPerTagList)
                {
                    OMenuController.DeleteOMenuPermissionByTag(MenuId, temp);
                }
            }
            else
            {
                List<string> newPerTagList = newPerTag.Split(new char[] { ',' }).ToList<string>();


                //1.从原权限与新权限对比
                foreach (string temp in oldPerTagList)
                {
                    if (!newPerTagList.Contains(temp))
                    {
                        //删除
                        OMenuController.DeleteOMenuPermissionByTag(MenuId, temp);
                    }
                }
                //2.从新的与原对比
                foreach (string temp in newPerTagList)
                {
                    if (!oldPerTagList.Contains(temp))
                    {
                        //新增
                        OMenuController.AddOMenuPermission(MenuId, temp);
                    }
                }

            }
            if (OMenuController.UpdateOMenuByMeunId(obj))
                SuccessJson("已成功修改菜单信息");
            else
                ErrorJson("修改菜单信息失败");

        }
    }
}